/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-7-5 下午4:46 Created by dwzhang
 */
package com.jade.framework.base.text;

import java.io.File;
import java.io.StringWriter;
import java.util.Map;
import java.util.Set;

import com.jade.framework.base.JadeSystem;
import com.jade.framework.base.util.Parameters;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

/**
 * Velocity工具类。
 *
 * @author dwzhang
 * @version 1.0 13-7-5 下午4:46
 */
public class VelocityUtils
{
    static {
        try {
            String props = JadeSystem.getProperty("velocity.properties", JadeSystem.getHome() + File.separator +
                                                                         "conf/system/velocity.properties");
            Velocity.init(props);
        }
        catch (Throwable t) {
            JadeSystem.getLog().warn("Can't init velocity:", t);
        }
    }

    public static String mergeTemplate(String template, Parameters params)
            throws Exception
    {
        return mergeTemplate(template, "UTF-8", params);
    }

    public static String mergeTemplate(String template, String encoding, Parameters params)
            throws Exception
    {
        return mergeTemplate(template, null, encoding, params);
    }

    public static String mergeTemplate(String template, String language, String encoding, Parameters params)
            throws Exception
    {
        if (!template.endsWith(".vm")) {
            if (StringUtils.isNotBlank(language)) {
                template += "_" + language;
            }
            template += ".vm";
        }

        VelocityContext context = new ParamsVelocityContext(params);
        StringWriter w = new StringWriter();
        Velocity.mergeTemplate(template, encoding, context, w);
        return w.toString();
    }

    public static String mergeContent(String pattern, String[] params)
            throws Exception
    {
        return mergeContent(pattern, params, true);
    }

    public static String mergeContent(String pattern, String[] params, boolean zero)
            throws Exception
    {
        return mergeContent(pattern, params, "P", zero);
    }

    public static String mergeContent(String pattern, String[] params, String paramPrefix, boolean zero)
            throws Exception
    {
        VelocityContext context = new VelocityContext();
        for (int i = 0; i < params.length; i++) {
            context.put(paramPrefix + String.valueOf(zero ? i : (i + 1)), params[i]);
        }
        StringWriter w = new StringWriter();
        Velocity.evaluate(context, w, "frame", pattern);
        return w.toString();
    }

    public static String mergeContent(String pattern, Parameters params)
            throws Exception
    {
        VelocityContext context = new ParamsVelocityContext(params);
        StringWriter w = new StringWriter();
        Velocity.evaluate(context, w, "frame", pattern);
        return w.toString();
    }

    public static String mergeContent(String pattern, Map<String, ? extends Object> params)
            throws Exception
    {
        VelocityContext context = new VelocityContext();
        Set<String> keys = params.keySet();
        for (String key : keys) {
            context.put(key, params.get(key));
        }
        StringWriter w = new StringWriter();
        Velocity.evaluate(context, w, "frame", pattern);
        return w.toString();
    }
}
